google.setOnLoadCallback(function() {
	$('body').ready(function(){
		getUsage("OFF_PEAK");
		getUsage("PEAK");
		updateBadgeCounter();
		
		$("#link_refresh").click(function(){
			showLoading();
			$("#error").empty();
			showContent("#usage");
			getUsage("OFF_PEAK");
			getUsage("PEAK");
			updateBadgeCounter();
			hideLoading();
		});

		$("#link_logout").click(function(){
			showLoading();
			$("#error").empty();
			$.ajax({
				url: "https://www.unwired.com.au/myaccount/logout.php",
				type: "POST",
				data: "",
				dataType: "html",
				success: function(){
					updateLoginForm();
					showContent("#loginForm");
					hideLoading();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					$("#error").empty().append("error: " + textStatus);
					hideLoading();
				}
			});
		});

		$("#loginForm input[type=password]").keypress(function(event){
			if(event.keyCode == 13)
			{
				$("#loginForm input[type=button]").click();
			}
		});

		$("#loginForm input[type=button]").click(function(){
			showLoading();
			$("#error").empty();
			$.ajax({
				url: "https://www.unwired.com.au/myaccount/login.php?ev=nx",
				type: "POST",
				data: $("#loginForm form").serializeArray(),
				dataType: "html",
				success: function(html){
					if($(".errorMsg", html).length > 0)
					{
						$(".errorMsg", html).each(function(){
							$("#error").append(this);
						});
					}
					else
					{
						if(localStorage.getItem("rememberMe"))
						{
							localStorage.setItem("userName", $("#loginForm input[name=login_name]").val());
						}
						else
						{
							localStorage.removeItem("userName");
						}
						
						showContent("#usage");
						getUsage("OFF_PEAK");
						getUsage("PEAK");
					}
					
					$("#loginForm form").each(function(){ this.reset(); });
					hideLoading();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					$("#error").empty().append("error: " + textStatus);
					$("#loginForm form").each(function(){ this.reset(); });
					hideLoading();
				}
			});
		});

		$("#forgotYourPassword").click(function(){
			chrome.tabs.create({
				url: "https://www.unwired.com.au/myaccount/forgotpassword.php"
			});
		});
		
		$("#logo").click(function(){
			chrome.tabs.create({
				url: "https://www.unwired.com.au/"
			});
		});
		
		$("#OFF_PEAK h2").click(function(){
			chrome.tabs.create({
				url: "https://www.unwired.com.au/myaccount/usage.php?view=OFF_PEAK"
			});
		});
		
		$("#PEAK h2").click(function(){
			chrome.tabs.create({
				url: "https://www.unwired.com.au/myaccount/usage.php?view=PEAK"
			});
		});
		
		$("#loginForm input[name=rememberMe]").click(function(){
			var $el = $(this);
			if($el.attr("checked"))
			{
				localStorage.setItem("rememberMe", true);
			}
			else
			{
				localStorage.removeItem("rememberMe");
			}
		});
		
		$("#link_about a").click(function(){
			showContent("#about");
		});
		
		$("#about input[type=button]").click(function(){
			showContent("#loginForm");
		});
		
		$("#link_sourcecode").click(function(){
			chrome.tabs.create({
				url: "https://code.google.com/p/unwired-usage/"
			});
		});
	});
});

function getUsage(type)
{
	$("#" + type + " .loading").css("display", "block");
	$.ajax({
		url: "https://www.unwired.com.au/myaccount/usage.php?view=" + type,
		type: "GET",
		data: "",
		dataType: "html",
		success: function (html){
			if($("form[name=loginForm]", html).length > 0)
			{
				updateLoginForm();
				showContent("#loginForm");
			}
			else
			{
				$("#" + type + " .loading").css("display", "none");
				
				// plan
				$("#plan").empty().append($(".usageTable:first tr:first", html));
				
				// time
				$("#" + type + "_time").empty().append($(".content p.small:first", html));
				
				// progress
				var $typePlan = null;
				$(".usageTable:first tr", html).each(function(index){
					if(index == 1)
					{
						$typePlan = $(this);
					}
				});
				var usageProgress = getUsageProgress(html);
				var progressPercentage = usageProgress.progressPercentage;
				var dayPercentage = usageProgress.dayPercentage;
				if(progressPercentage > dayPercentage)
				{
					$("#" + type + " .progress").addClass("over");
				}
				else
				{
					$("#" + type + " .progress").removeClass("over");
				}
				$("#" + type + " .progress").html("<span style='font-size:70%;'>" + dayPercentage.toPrecision(4) + " %</span>&nbsp;" + progressPercentage.toPrecision(4) + " %");
				
				// usage
				var $usage = $(".usageTable:last", html);
				$("#" + type + " table").empty().append($typePlan).append($usage);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			$("#error").append("error: " + textStatus);
			$("#" + type + " .loading").css("display", "none");
			$("#" + type + " table").empty();
		}
	});
}

function updateLoginForm()
{
	if(localStorage.getItem("rememberMe"))
	{
		$("#loginForm input[type=checkbox]").attr("checked", "checked");
		var userName = localStorage.getItem("userName");
		if(userName)
		{
			$("#loginForm input[name=login_name]").val(userName);
		}
	}
	else
	{
		localStorage.removeItem("userName");
	}
}

function showLoading()
{
	$("#loading").css("display", "block");
}

function hideLoading()
{
	$("#loading").css("display", "none");
}

function showContent(contentSelector)
{
	$("#content").children().css("display", "none");
	$(contentSelector).css("display", "block");
}